package weekMatch.one_339;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

public class LeeCode_02 {
    public static void main(String[] args) {
//        System.out.println(primeSubOperation(new int[]{4, 9, 6, 10}));
//        System.out.println(primeSubOperation(new int[]{6,8,11,12}));
//        System.out.println(primeSubOperation(new int[]{5,8,3}));
//        System.out.println(primeSubOperation(new int[]{2,2}));
        System.out.println(primeSubOperation(new int[]{998,2}));
    }
    private static boolean primeSubOperation(int[] nums) {
        List<Integer> list = new ArrayList<>();
        getzhi(list, 2);
        for (int i = 0; i < nums.length; i++) {
            int index = -1;
            for (int j = 0; j < list.size(); j++) {
                if (list.get(j) >= nums[i]){
                    index = j - 1;
                    break;
                }
            }
            if (index == -1){
                if (i > 0 && nums[i] <= nums[i - 1]){
                    return false;
                }
                continue;
            }
            if (i == 0){
                nums[i] -= list.get(index);
                continue;
            }
            for(int j = index; j >= 0; j--){
                if (nums[i] - list.get(j) > nums[i - 1]){
                    nums[i] -= list.get(j);
                    break;
                }
            }
            if (nums[i] <= nums[i - 1]){
                return false;
            }
        }
        return true;
    }
    private static void getzhi(List<Integer> list, int n){
        if (n == 1010)
            return;
        double mid = Math.pow(n, 0.5);
        boolean flag = true;
        for (int i = 2; i <= mid; i++) {
            if (n % i == 0){
                flag = false;
                break;
            }
        }
        if (flag)
            list.add(n);
        getzhi(list, n + 1);
    }
}
